<?php
/**
 * Personnal Information
 * such as address, phone...
 *
 * @package hive
 * @subpackage ressource
 */
use Fox\Generator;
use Hive\Ressource;
use Hive\School;
use Fox\Dao\SqlFragment;
use Hive\Ressource\Source;
use Fox\Html\Header\Content;
use Fox\Core\Errors;
use Fox\Html\Screen;
$xview =  Xview::instance();

/* @var $contact Hive\Ressource */
$contact = $xview->component('ressource', 'object');
// Customizing
$customizing = Hive\Ressource\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'ressource','act'=>'personnal'));
$data = $contact->getData();

$screen = new Screen('resUpdate','update',null,true);
$screen->action(Generator::url('ressource','personnal',null,array('rid' => $contact->getid())));
$last_update = Msg(Fox\Msg::BUNDLE,Fox\Msg::UPDATED_ON_BY,
					Fox\Utilities\DateTimes::toFullString($contact->udate),
					User::fullName($contact->uname));
$last_update = '<br><span class="i sub-title-mini"> '.$last_update.'</span>';
$title = new Content('personnal',Msg(Hive::BUNDLE,Hive::LABEL_PERSONNAL_DATA).$last_update);
$screen->mainTitle($title->render());
$blHeader = $xview->component('ressource', 'header');
$screen->add($blHeader);
// Set Sources
$sources = array();
foreach($contact->sources() as $source) {
	$data['social_'.$source->type] = $source->reference;
	$sources[$source->type] = $source;
	$type = Fox\Dao::load(Source\Type::className(),$source->type);
	$uri =  $xview->val('social_'.$source->type,$source->reference);
	if(!empty($uri)) {
		$screen->item('social_'.$source->type)
				->add(new Fox\Html\Html('social_icon_'.$source->type,
					$type->render($uri)
		
		));
	}
}
$screen->initialize($data);

if($screen->submitted())
{
	$fields = $screen->get();
	if(!$screen->errors())
	{
		try {
			// Check if the school already exist
			if(!empty($fields['school']))
			{
				$fields['school'] = trim($fields['school']);
				$school = School::lookup($fields['school']);
				if(!$school)
				{
					$school = new School();
					$school->setFields(array('sch_name' => $fields['school']));
					$school->create();
				}
				$fields['schoolid'] = $school->getId();
			}
			if(isset($fields['kn_udate'])) {
				unset($fields['kn_udate']);
			}
			$contact->setFields($fields);
			$contact->prepareUpdate(Hive\Ressource::CHANGE_DEFAULT);
			// Manage Social Information
			$finder = new Hive\Ressource\Source\Type();
			$sTypes = $finder->find(new SqlFragment('active = ? ',array(1)));
			foreach($finder->find() as $type){
				if(isset($sources[$type->type])) {
					$sources[$type->type]->reference = $fields['social_'.$type->type];
					$contact->replaceSources($sources[$type->type]);
				} elseif(!empty($fields['social_'.$type->type])) {
					$source = new Source();
					$source->setFields(array('type' => $type->type,'reference' => $fields['social_'.$type->type]));
					$contact->addToSources($source);
				}
			}
			if($contact->update()) {
				Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,$contact->name()));
			}
		}
		catch (Fox\Object\Exception\Save $e)
		{
			Errors::generateMessage($e);
		}

	}
	echo $xview->messages(true,true);
	exit;
}
echo $screen->display();
?>